package org.rxc.gift.service.impl;

import com.baomidou.mybatisplus.annotations.TableField;
import io.seata.spring.annotation.GlobalTransactional;
import org.rxc.gift.clients.AuthClient;
import org.rxc.gift.config.RegisterNoticeRabbitmqConfig;
import org.rxc.gift.domain.LoginUser;
import org.rxc.gift.domain.User;
import org.rxc.gift.domain.UserInfo;
import org.rxc.gift.dto.UserRegisterNoticePhoneDTO;
import org.rxc.gift.dto.UserRegisterPhoneDTO;
import org.rxc.gift.exception.BusinessException;
import org.rxc.gift.exception.ResponseCode;
import org.rxc.gift.exception.RonghuaAssert;
import org.rxc.gift.mapper.UserMapper;
import org.rxc.gift.service.IUserInfoService;
import org.rxc.gift.service.IUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.rxc.gift.service.IVerifyCodeService;
import org.rxc.gift.util.AjaxResult;
import org.rxc.gift.util.encrypt.MD5;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author rxc
 * @since 2024-05-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    @Autowired
    private IVerifyCodeService verifyCodeService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private AuthClient authClient;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void registerPhone(UserRegisterPhoneDTO dto) {
        //1.参数校验
        //验证码校验
        //通过手机号去redis中获取验证码
        String smsCodeByRedis = verifyCodeService.getSmsCode(dto.getPhone());
        //再与前端传过来的验证码作比较
        RonghuaAssert.isEqIgnoreCase(smsCodeByRedis,dto.getValidCode(), ResponseCode.RESPONSE_CODE_108);

        //2.添加数据到数据库
        //密码加密加盐处理
        String salt = UUID.randomUUID().toString().replaceAll("-","");
        String password = MD5.getMD5("ronghua"+salt+dto.getPassword()+"okok");
        //远程调用授权中心,保存数据到t_login_user表,获取id
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername("rh_"+UUID.randomUUID().toString().replaceAll("-",""));
        loginUser.setSalt(salt);
        loginUser.setPassword(password);
        loginUser.setPhone(dto.getPhone());
        loginUser.setType(1);
        AjaxResult ajaxResult = authClient.addOrUpdate(loginUser);
        //判断
        if (!ajaxResult.isSuccess()){
            throw new BusinessException(ajaxResult.getMessage());
        }
        Long loginId = Long.parseLong(ajaxResult.getResultObj().toString());

        //通过获取到的login_id添加数据到t_user
        User user = new User();
        user.setCreateTime(new Date().getTime());
        user.setPhone(dto.getPhone());
        user.setSalt(salt);
        user.setPassword(password);
        user.setBitState(1L);
        user.setLoginId(loginId);
        insert(user);
        //添加数据到t_user_info
        UserInfo userInfo = new UserInfo();
        userInfo.setCreateTime(new Date().getTime());
        userInfo.setRegTime(userInfo.getCreateTime());
        userInfo.setLevel(0);
        userInfo.setGrowScore(0);
        userInfo.setUserId(user.getId());
        userInfoService.insert(userInfo);

        //3.发送短信/邮件通知用户注册成功
        String content = "恭喜你,注册成功！";
        UserRegisterNoticePhoneDTO phoneDTO = new UserRegisterNoticePhoneDTO(dto.getPhone(), content);
        rabbitTemplate.convertAndSend(RegisterNoticeRabbitmqConfig.EXCHANGE_TOPICS_INFORM,"inform.sms",phoneDTO);
    }
}
